package tk.mybatis.springboot;

import org.apache.log4j.Logger;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.http.HttpServletRequest;
import java.security.AllPermission;

/**
 * @author liuzh
 * @since 2015-12-12 18:22
 */
@Controller
@EnableWebMvc
@SpringBootApplication
@MapperScan(basePackages = "tk.mybatis.springboot.mapper")
@EnableEurekaClient
@RestController
@EnableDiscoveryClient
public class Application extends WebMvcConfigurerAdapter {

    private final Logger logger = Logger.getLogger(Application.class);

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

	@Value("${spring.datasource.druid.max-active}")
	String port;
	@RequestMapping("/hi")
	public String home(@RequestParam String name) {
		return "hi "+name+",i am from wwport:" +port;
	}
	
  @RequestMapping("/")
    public String home() {
        return "Hello world";
    }

    @RequestMapping(value = "/trace-2", method = RequestMethod.GET)
    public String tarce(HttpServletRequest request){
	    logger.info("====<call trace-2, TraceId={}, SpanId={}>=====");
	    //,request.getHeader("X-B3-TraceId"), request.getHeader("X-B3-SpanId")
        return "Trace";
    }

    @Bean
    @LoadBalanced
    RestTemplate getRestTemplate(){
        return new RestTemplate();
    }

    @RequestMapping(value="/zipkin-server-2",method = RequestMethod.GET)
    public String trace(){
        logger.info("====call zipkin-server-1 =====");
        return getRestTemplate().getForObject("http://trace-2/trace-2", String.class);
    }
    
    
}
